﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GBH.Models.Dao
{
    public class BranchDao
    {
        public List<Branch> GetAllActiveBranch()
        {
            using (var db = new GBHEntities())
            {
                var branchs = (from b in db.Branches
                               select b).ToList();
                return branchs;
            }
        }

        public string GetCusNoByBranchId(string branchId)
        {
            using (var db = new GBHEntities())
            {
                var branchs = (from b in db.Branches
                               where b.mprovtrans == branchId
                               select b.mcustno).FirstOrDefault();
                return branchs;
            }
        }

        public Branch GetBranchByBranchCode(String branchCode)
        {
            using (var db = new GBHEntities())
            {
                var branch = (from b in db.Branches
                               where b.mprovtrans == branchCode
                               select b).FirstOrDefault();
                return branch;
            }
        }

        public Branch GetBranchByBranchId(string mbranchno)
        {
            using (var db = new GBHEntities())
            {
                var branch = (from b in db.Branches
                              where b.mbranchno == mbranchno
                              select b).FirstOrDefault();
                return branch;
            }
        }

        public List<Branch> GetBranchByBranchIdOrName(string mbranchno, string mbranchname)
        {
            using (var db = new GBHEntities())
            {
                var branch = from b in db.Branches
                             select b;
                if (!string.IsNullOrEmpty(mbranchno))
                {
                    branch = branch.Where(b => b.mbranchno.Contains(mbranchno.Trim()));
                }

                if (!string.IsNullOrEmpty(mbranchname))
                {
                    branch = branch.Where(b => b.mprovdesc.Contains(mbranchname.Trim()));
                }

                return branch.ToList();
            }
        }

        public bool SaveBranchs(List<Branch> branchs)
        {
            try
            {
                foreach (var branch in branchs)
                {
                    SaveBranch(branch);
                }
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }

        }

        public bool SaveBranch(Branch Branch)
        {
            using (var db = new GBHEntities())
            {
                var existingBranch = db.Branches.Where(b => b.mbranchno == Branch.mbranchno).FirstOrDefault();
                if (existingBranch != null)
                {
                    existingBranch.mbranchno = Branch.mbranchno;
                    existingBranch.mcustno = Branch.mcustno == null ? "" :Branch.mcustno.Trim();
                    existingBranch.mprovdesc = Branch.mprovdesc == null ? "" : Branch.mprovdesc.Trim();
                    existingBranch.mprovice = "";
                    existingBranch.mprovtran_grp = Branch.mprovtran_grp;
                    existingBranch.mprovtrans = Branch.mprovtrans == null ? "" : Branch.mprovtrans.Trim();
                    existingBranch.mprovtransdesc = Branch.mprovtransdesc;
                    existingBranch.isenable = Branch.isenable;
                }
                else
                {
                    db.Branches.Add(Branch);
                }
                return db.SaveChanges() > 0;
            }
        }

        public bool InsertBranch(Branch NewBranch)
        {
            using (var db = new GBHEntities())
            {
                db.Branches.Add(NewBranch);
                int result = db.SaveChanges();
                return result > 0;
            }
        }

        public bool UpdateBranch(Branch EditBranch)
        {
            using (var db = new GBHEntities())
            {
                var exitingBranch = db.Branches.Where(b => b.id == EditBranch.id).FirstOrDefault();
                if (exitingBranch != null)
                {
                    exitingBranch = EditBranch;
                    int result = db.SaveChanges();
                    return result > 0;
                }
                return false;
            }
        }
        public bool ClearBranch()
        {
            using (var db = new GBHEntities())
            {
                var branch = db.Database.ExecuteSqlCommand("TRUNCATE TABLE [Branch]");
                return branch > 0;
            }
        }

        public bool DeleteBranch(int BranchId)
        {
            using (var db = new GBHEntities())
            {
                var branch = db.Branches.Where(b => b.id == BranchId).FirstOrDefault();
                if (branch != null)
                {
                    db.Branches.Remove(branch);
                    int result = db.SaveChanges();
                    return result > 0;
                }
                return false;
            }
        }

        public bool DeleteBranch(Branch Branch)
        {
            using (var db = new GBHEntities())
            {
                db.Branches.Remove(Branch);
                int result = db.SaveChanges();
                return result > 0;
            }
        }
    }
}